package com.biconn.bigdata;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.biconn.bigdata.common.md5.MD5Util;
import com.biconn.bigdata.common.util.DateUtils;
import com.biconn.bigdata.common.util.StringUtils;
import com.biconn.bigdata.es.model.QuestionAndAnswerOnline;
import com.biconn.bigdata.es.repository.BaseElasticsearchRepository;

@Component
public class CustomCommandLineRunner{
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private MongoTemplate mongoTemplate;
	@Autowired
	private BaseElasticsearchRepository<QuestionAndAnswerOnline> baseElasticsearchRepository;

	//删除ES既有数据，同步mongo数据至ES
	@Async
	public void deleteDocumentsAndUploadData2ES(){
		baseElasticsearchRepository.delet("question_and_answer_realtime_index");
		uploadData2ES();
	}
	public void uploadData2ES() {
		List<QuestionAndAnswerOnline> questionAndAnswerOntimeList = new ArrayList<>();
		int page = 0;
		int number = 5000;
		Query query = new Query();
		query.limit(number);
		while(true) {
			query.skip(number*page);
			List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "question_and_answer_realtime_index");
			if(list.size()<1) {
				break;
			}
			page++;
			for(JSONObject value : list) {
				try {
					QuestionAndAnswerOnline questionAndAnswerOnline = new QuestionAndAnswerOnline();
					questionAndAnswerOnline.setAnswerContent(value.getJSONArray("answerContent").toString());
					questionAndAnswerOnline.setAskContentMd5(value.getString("askContentMd5"));
					questionAndAnswerOnline.setCreateTime(value.getString("createTime"));
					questionAndAnswerOnline.setCustomerAge(value.getString("customerAge"));
					questionAndAnswerOnline.setCustomerSex(value.getString("customerSex"));
					questionAndAnswerOnline.setQuestionContent(value.getString("questionContent"));
					questionAndAnswerOnline.setQuestionDepartment(value.getString("questionDepartment"));
					//questionAndAnswerOnline.setQuestionId(value.getString("id"));
					questionAndAnswerOnline.setQuestionSource(value.getString("questionSource"));
					questionAndAnswerOnline.setQuestionTitle(value.getString("questionTitle"));
					questionAndAnswerOnline.setQuestionUrl(value.getString("questionUrl"));
					
					questionAndAnswerOnline.setUpdateTime(value.getString("updateTime"));
					questionAndAnswerOnline.setSaveTime(value.getString("saveTime"));
					questionAndAnswerOntimeList.add(questionAndAnswerOnline);
				} catch (Exception e) {
					logger.info("answers转数组异常");
				}
				
			}
			try {
				baseElasticsearchRepository.batch(questionAndAnswerOntimeList, "question_and_answer_realtime_index", "questionAndAnswerRealtime");
				//baseElasticsearchRepository.batch(questionAndAnswerOntimeList, "question_and_answer_realtime_index_test", "questionAndAnswerRealtime");
				logger.info("批量持久化数据数量：" + questionAndAnswerOntimeList.size());
				questionAndAnswerOntimeList.clear();
			} catch (Exception e) {
				// TODO: handle exception
				logger.info("批量持久化失败");
			}
			
		}
	}
	//处理之前mysql数据
	public void dealPreviousData() {
		int page = 0;
		int number = 100;
		Query query = new Query();
		query.limit(number);
		while(true) {
			query.skip(number*page);
			List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "question_and_answer_index_stop");
			if(list.size()<1) {
				break;
			}
			page++;
			for(JSONObject value : list) {
				try {
					JSONObject thisItem = new JSONObject();
					thisItem.put("questionContent", value.getString("ask_content"));
					thisItem.put("questionUrl", value.getString("ask_link"));
					thisItem.put("customerAge", value.getString("ask_age"));
					thisItem.put("askContentMd5", value.getString("askContentMd5"));
					thisItem.put("questionTitle", value.getString("ask_title"));
					thisItem.put("questionDepartment", value.getString("ask_department"));
					thisItem.put("createTime", value.getString("save_time"));
					thisItem.put("customerSex", value.getString("ask_sex"));
					thisItem.put("questionSource", value.getString("source_web"));
					JSONArray newarray = new JSONArray();
					JSONArray array = value.getJSONArray("answers");
					for(int i=0;i<array.size();i++) {
						JSONObject newItem = new JSONObject();
						JSONObject item = array.getJSONObject(i);
						newItem.put("answer_time", item.getString("doctor_time"));
						newItem.put("doctor_img", "");
						newItem.put("answer_content", item.getString("doctor_answer"));
						String info = item.getString("doctorinfo");
						if(info.contains("驻站医生")) {
							newItem.put("doctor_pos", "驻站医生");
						}else {newItem.put("doctor_pos", "");}
						if(info.contains("全科")) {
							newItem.put("doctor_good", "全科");
						}else {
							newItem.put("doctor_good", "全科");
						}
						
						newItem.put("doctor_hos", "");
						newItem.put("doctor_name", "");
						newItem.put("doctor_dep", "");
						newarray.add(newItem);
					}
					if(newarray.size()<1) {continue;}
					thisItem.put("answerContent", newarray);
					logger.info(thisItem.toJSONString());
					mongoTemplate.insert(thisItem, "question_and_answer_realtime_index");
				} catch (Exception e) {
					logger.info("answers转数组异常");
				}
				
			}
		}
	}
	
	//mongo数据备份   
	@Async
	public void questionAndAnswerOntimeDataBackup() {
		String backupTableName = "question_and_answer_realtime_index_backup_"+DateUtils.dateTimeNow();
		int page = 0;
		int number = 5000;
		Query query = new Query();
		query.limit(number);
		while(true) {
			query.skip(number*page);
			List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "question_and_answer_realtime_index");
			if(list.size()<1) {
				break;
			}
			page++;
			for(JSONObject value : list) {
				try {
					value.remove("_id");
					value.remove("id");
					String questionConstent = value.getString("questionContent");
					value.put("askContentMd5", MD5Util.encode(questionConstent));
					logger.info(value.toJSONString());
					mongoTemplate.insert(value, backupTableName);
				} catch (Exception e) {
					logger.info("answers转数组异常");
				}
				
			}
		}
	}
	
		//mongo数据迁移  
		@Async
		public void questionAndAnswerOntimeDataTransfer(String fromDb, String toDb) {
			int page = 0;
			int number = 5000;
			Query query = new Query();
			query.limit(number);
			while(true) {
				query.skip(number*page);
				List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, fromDb);
				if(list.size()<1) {
					break;
				}
				page++;
				for(JSONObject value : list) {
					try {
						value.remove("_id");
						value.remove("id");
						logger.info(value.toJSONString());
						mongoTemplate.insert(value, toDb);
					} catch (Exception e) {
						logger.info("answers转数组异常");
					}
					
				}
			}
		}
	
		//mongo数据更新
		public void updateMongo() {
			Query query = new Query();
			Criteria criteria=Criteria.where("questionSource").is("康爱多健康问答");
			query.addCriteria(criteria);
			Update update = Update.update("createTime", DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS));
			mongoTemplate.updateMulti(query, update, "question_and_answer_realtime_index");
		}
		
		
		//删除图片
		public void addSaveTimeAndDealPicImageUrl() {
			int page = 0;
			int number = 5000;
			Query query = new Query();
			query.limit(number);
			while(true) {
				query.skip(number*page);
				List<JSONObject> list = mongoTemplate.find(query, JSONObject.class, "question_and_answer_realtime_index");
				if(list.size()<1) {
					break;
				}
				page++;
				for(JSONObject value : list) {
					try {
						value.remove("_id");
						value.remove("id");
						
						questionTime(value);
						logger.info(value.toJSONString());
						mongoTemplate.insert(value, "question_and_answer_realtime_index_backup_add_field_20180912");
					} catch (Exception e) {
						logger.info("answers转数组异常");
					}
					
				}
			}
		}

		private void questionTime(JSONObject jsonMsg) {
			String questionTime = jsonMsg.getString("createTime");
			try {
				//抓取到的时间格式处理 先判断是否 yyyy-MM-ddHH:mm:ss 格式，是的话转换下格式不是的话异常处理，使用当前时间
				
				if(!StringUtils.isEmpty(questionTime)) {
					//YYYY_MM_DD  2018-09-12 
					if(questionTime.length()==10) {
						//do nothing
					}else if(questionTime.length()==18) {//2018-09-1208:34:21
						Date date = DateUtils.dateTime(DateUtils.YYYY_MM_DDHH_MM_SS, questionTime);
						jsonMsg.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, date));
					}else if(questionTime.length()==19) { //2018-09-12 08:34:21
						//do nothing
					}else {
						jsonMsg.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
					}
					
				}else {
					jsonMsg.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
				}
			} catch (Exception e) {
				jsonMsg.put("createTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
			}
			
			
			
			questionTime = jsonMsg.getString("createTime");
			JSONArray jsonArrayResult = new JSONArray();
			JSONArray jsonArray = jsonMsg.getJSONArray("answerContent");
			for(int i=0;i<jsonArray.size();i++) {
				JSONObject it = jsonArray.getJSONObject(i);
				it.put("doctor_img", "");
				jsonArrayResult.add(it);
			}
			jsonMsg.put("answerContent", jsonArrayResult);
			jsonMsg.put("saveTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
			jsonMsg.put("updateTime", DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, new Date()));
			
		}
}